<template>
  <a-modal
    title="操作"
    style="top: 100px"
    :width="1200"
    v-model="editVisible"
    @ok="handleEditOk"
    @cancel="cancelHandel"
    :maskClosable=false
    :bodyStyle="{ 'max-height': '500px', overflow: 'scroll' }"
    :confirmLoading="confirmLoading"
  >
    <a-card :bordered="false">
      <a-table
        ref="stable"
        :columns="columns"
        :dataSource="itemList"
        :loading="loading"
        :rowKey="row => row.id"
        :pagination="false"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }">
      </a-table>
    </a-card>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { insertItem, queryItem, queryItemPersonnel } from '@/api/parExamPlan'
export default {
  name: 'Rolelist',
  components: {
    STable
    // RoleAuth,
  },
  data () {
    return {
      // 编辑弹出框
      param: {},
      type: '',
      // 页码
      pagination: {
        total: 0,
        pageSize: 5, // 每页中显示5条数据
        showSizeChanger: true,
        pageSizeOptions: ['5', '10', '20', '50'], // 每页中显示的数据
        showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
      },
      loading: true,
      editVisible: false,
      confirmLoading: false,
      selectedRowKeys: [],
      selectedRows: [],
      queryParam: {
        dwlx: '',
        kpdl: '',
        id: '',
        pageNo: 1, // 第几页
        pageSize: 5// 每页中显示数据的条数
      },
      itemList: [],
      // 表头
      columns: [
        {
          title: '考评标准及要点大类',
          dataIndex: 'parentName',
          width: '400px'
        },
        {
          title: '考评内容',
          dataIndex: 'name'
        },
        {
          title: '考评总分',
          dataIndex: 'kpfs'
        },
        {
          title: '考评点',
          dataIndex: 'kpd',
          ellipsis: true
        },
        {
          title: '评分标准',
          dataIndex: 'kpbz',
          ellipsis: true
        }
      ]
    }
  },
  methods: {
    // 加载数据方法
    loadData (parameter) {
      this.loading = true
      if (this.type === 'org') {
        queryItem(parameter).then(res => {
          this.itemList = res.result.data
          const pagination = { ...this.pagination }
          pagination.total = res.result.totalCount
          this.pagination = pagination
          if (this.param.kpxm !== '' && this.param.kpxm !== null && this.param.kpxm !== undefined) {
            this.selectedRowKeys = this.param.kpxmid.split(',')
          } else {
            this.selectedRowKeys = []
            this.selectedRows = []
          }
          this.loading = false
        })
      } else if (this.type === 'personnel') {
        queryItemPersonnel(parameter).then(res => {
          this.itemList = res.result.data
          const pagination = { ...this.pagination }
          pagination.total = res.result.totalCount
          this.pagination = pagination
          if (this.param.kpxmid) {
            this.selectedRowKeys = this.param.kpxmid.split(',')
          } else {
            this.selectedRowKeys = []
            this.selectedRows = []
          }
          this.loading = false
        })
      }
    },
    handleTableChange (pagination) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.queryParam.pageNo = pagination.current
      this.queryParam.pageSize = pagination.pageSize
      this.loadData(this.queryParam)
    },
    begin: function (record, type) {
      this.type = type
      this.editVisible = true
      this.param = record
      this.queryParam.dwlx = record.dwlx
      this.queryParam.kpdl = record.kpgzfl
      this.queryParam.id = record.id
      this.loadData(this.queryParam)
      /* this.selectedRowKeys = []
      if (record.kpxmid) {
        this.selectedRowKeys = record.kpxmid.split(',')
      } else {
        this.selectedRowKeys = []
        this.selectedRows = []
      } */
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleEditOk () {
      const params = []
      this.selectedRowKeys.forEach((item, index) => {
        params.push({ kpxid: item, kpxmflid: this.param.id })
      })
      const paramsFnl = { kpxmflid: this.param.id, list: params }
      insertItem(paramsFnl)
        .then(res => {
          if (res.status === 200) {
            this.$notification['success']({
              message: '创建考评项成功',
              duration: 2
            })
            // 关闭编辑框
            this.editVisible = false
            // 刷新列表
            // this.$refs.stable.refresh(true)
            this.$emit('success', { selectedRows: this.selectedRows, selectedRowKeys: this.selectedRowKeys })
          } else {
            this.$notification['error']({
              message: res.message,
              duration: 4
            })
          }
        })
        .catch(() =>
          this.$notification['error']({
            message: '出现异常，请联系系统管理员',
            duration: 4
          })
        )
        .finally(() => {
          this.confirmLoading = false
        })
    },
    /**
     * 关闭弹出框
     */
    close () {
      this.selectedRowKeys = []
      this.selectedRows = []
      this.editVisible = false
      this.itemList = []
      this.pagination = {
        total: 0,
        current: 1,
        pageSize: 5, // 每页中显示5条数据
        showSizeChanger: true,
        pageSizeOptions: ['5', '10', '20', '50'], // 每页中显示的数据
        showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
      }
    },
    /**
     * 取消操作
     */
    cancelHandel () {
      this.close()
    }
  }
}
</script>
<style lang="less" scoped>
</style>
